var update;
$(document).ready(function(){
	pcs.common.getUserSession(validatePermission,true);
	getParaList();
	$('#subImg').bind('click',subButton);
	$('#cancel').bind('click',function(){$('#setForm').hide()});
	validate();
})

//验证权限
function validatePermission(userinfo){
	var permissionNames = "update";
	var actionNames = "UPDATE_T_SYS_PARA_ACTION";
	pcs.common.hasPermission(permissionNames,actionNames,isShow);
}
//是否显示操作按钮
function isShow(data){
	var obj = data.root.PERMISSION.rs[0];
	update = obj.update;
}

function getParaList(){
	var url = pcs.common.path + '/json?action=QUERY_T_SYS_PARABY_PAGER_ACTION';
	var xmlname = "T_SYS_PARA";
	var pagePanel='#pager';
	var result='#list';
	var pageSize=20;
	var defaultHtml='<li><em style="text-align: center;width:100%">没有搜索到您要查询的数据...</em></li>';
	pcs.common.changePage(url,pageSize,xmlname,result,fn_getRoleList,pagePanel,defaultHtml);
}

function fn_getRoleList(obj,result,i){
	var str = "<li>";
	str += "<em style='width: 15%; text-align: center;'>"+obj.SYS_PARA_CODE+"</em>";
	str += "<em style='width: 20%; text-align: center;'>"+obj.SYS_PARA_NAME+"</em>";
	str += "<em style='width: 20%; text-align: center;'>"+obj.PARA_VALUES+"</em>";
	str += "<em style='width: 34%; text-align: center;'>"+obj.SYS_PARA_DESC+"</em>";
	str += "<em style='width: 10%; text-align: center;'>";
	if(update){
		str += "<a href='javascript:setPara(\""+obj.SYS_PARA_CODE+"\",\""+obj.SYS_PARA_NAME+"\",\""+obj.PARA_VALUES+"\",\""+obj.SYS_PARA_DESC+"\")'>设置</a>";
	}
	else{
		str += "您无操作权限";
	}
	str += "</em></li>";
	$(result).append(str);
}

function setPara(code, name, value, desc){
	$('#PARA_CODE').html(code);
	$('#SYS_PARA_CODE').attr('value',code);
	$('#SYS_PARA_NAME').html(name);
	$('#PARA_VALUES').attr('value',value);
	$('#SYS_PARA_DESC').attr('value',desc);
	$('#setForm').show();
}

function subFrm(){
	var options = {
		url : pcs.common.path + '/json?action=UPDATE_T_SYS_PARA_ACTION',
	    dataType: 'json',
    	iframe:	false,
	    success: function(data) {
	    	if(data.success){
	    		alert('配置成功！');
	      		pcs.common.getUserSession(getParaList,true);
	      	}else{
	      		alert(data.errors.errmsg);
	      	}
	   	}
	};
	$.ajaxSetup({contentType:'application/x-www-form-urlencoded;charset=utf-8'});
    $('#frm').ajaxSubmit(options);
}

function subButton(){
	return jQuery.formValidator.pageIsValid(1);
}

function validate(){
	$.formValidator.initConfig({formid:"frm",onerror:function(msg){alert(msg);},onsuccess:function(){subFrm();return false;}});	
	$("#PARA_VALUES").formValidator({onshow:"请输入缺省值",oncorrect:"正确",onfocus:"请输入缺省值"})
	.inputValidator({min:1,onerror:"请输入缺省值"}).inputValidator({max:128,onerror:"缺省值字符数不超过128"})
	.functionValidator({
		fun:function (str,elem){
			var id = $('#SYS_PARA_CODE').val();
			str = $.trim(str);
			switch(id){
				case '1001' : 
							   var a = !str.match("^[1-9]{1}[0-9]{0,1}$");
							   if(a) return "登录次数不小于1，且为1-2位的数字";break;
				case '1002' : 
							   var a = !str.match("^[0-9]+:[0-5]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$") ;
							   if(a) return "时间格式错误，正确为12:00:00";break;
				case '1003' : 
							   var a = !str.match("^[0-9]+$") ;
							   if(a) return "积分只能为数字";break;
				case '1004' : 
							   var a = !str.match("^[1-9]{1}[0-9]{0,1}$") ;
							   if(a) return "最新广告条数只能为数字,值为1-99,不能为0";break;
				case '1005' : 
							   var a = !str.match("^[1-9]{1}[0-9]{0,1}$") ;
							   if(a) return "最热广告条数只能为数字,值为1-99,不能为0";break;
				case '1006' : 
							   var a = !str.match("^[0-1]+.[0-9]{1,2}$");
							   if(a) return "9#服务费用数值范围为0-1之间的2位小数";break;
				case '1007' :
							   var a = !str.match("^[0-9]{1,10}$");
							   if(a) return "预缴纳费用下限值只能为数字,值在10位数以内";break;
				case '1008' :
							   var a = !str.match("^[1-9]{1}[0-9]{0,4}$");
							   if(a) return "有效接听时长只能为数字,值在5位数以内,不能为0";break;
				case '1009' :
							   var a = !str.match("^[1-9]{1}[0-9]{0,1}$");
							   if(a) return "有效播放上限次数为数字,值在2位数以内,不能为0";break;
				case '1010' :
							   var a = !str.match("^[1-9]{1}[0-9]{0,2}$");
							   if(a) return "预投放时间到达提醒天数必须为数字,值在3位数以内,不能为0";break;
				case '1011' :
							   var a = !str.match("^([1-9]{1}[0-9]{0,1}|100)$");
							   if(a) return "预投放人次到达提醒百分比,值在1-100以内,不能为0";break;
				case '1012'	 :
							   var a = !str.match("^[0-9]+$") ;
							   if(a) return "积分只能为数字";break;
				case '1013'	 :
							   var a = !str.match("^[0-9]+$") ;
							   if(a) return "推荐铃音数只能为数字";break;
				case '1014'	 :
							   var a = !str.match("^[0-9]+$") ;
							   if(a) return "默认铃音数只能为数字";break;
			}
			return true;
		}, onerror: "缺省值格式不正确"
	})
	$("#SYS_PARA_DESC").formValidator({onshow:"请输入备注",oncorrect:"正确",onfocus:"请输入备注"}).inputValidator({max:256,onerror:"缺省值字符数不超过256"})
	
}
